<?php


namespace tmcore\services\notice;

use tmcore\services\notice\applet\AppletSendService;
use tmcore\services\notice\mail\MailSendService;
use tmcore\services\notice\sms\SmsSendService;
use tmcore\services\notice\system\SystemSendService;
use tmcore\services\notice\wechat\WechatSendService;
use app\common\model\notice\NoticeConfig;
use app\common\model\notice\UserNoticeLog;
use tmcore\traits\ErrorTrait;
use tmcore\utils\Helper;

class NoticeService
{
    use ErrorTrait;

    /**
     * 发送消息
     * @param string $scene
     * @param array $params
     * @param bool $is_async
     * @return bool
     */
    public static function send($scene, $params = [], $is_async = false)
    {
        $config = NoticeConfig::where(['scene'=>$scene])->findOrEmpty()->toArray();
        if(!$config) return false;

        $config['msg_token'] = Helper::get_random(16);

        $notice_result = true;

        // 站内信
        if($config['is_system'] == 1 && $config['system_template'] && isset($params['system'])) {
            $result = (new SystemSendService)->send($config, $params['system'], $is_async);
            if(false == $result) {
                self::setError(SystemSendService::getError());
                $notice_result = false;
            }
        }

        // 短信
        if($config['is_sms'] == 1 && $config['sms_template'] && isset($params['sms'])) {
            $result = (new SmsSendService)->send($config, $params['sms'], $is_async);
            if(false == $result) {
                self::setError(SmsSendService::getError());
                $notice_result = false;
            }
        }

        // 邮件
        if($config['is_mail'] == 1 && $config['mail_template'] && isset($params['mail'])) {
            $result = (new MailSendService)->send($config, $params['mail'], $is_async);
            if(false == $result) {
                self::setError(MailSendService::getError());
                $notice_result = false;
            }
        }

        // 微信模板消息
        if($config['is_wechat'] == 1 && $config['wechat_template'] && isset($params['wechat'])) {
            $result = (new WechatSendService)->send($config, $params['wechat'], $is_async);
            if(false == $result) {
                self::setError(WechatSendService::getError());
                $notice_result = false;
            }
        }

        // 小程序订阅
        if($config['is_applet'] == 1 && $config['applet_template'] && isset($params['applet'])) {
            $result = (new AppletSendService)->send($config, $params['applet'], $is_async);
            if(false == $result) {
                self::setError(AppletSendService::getError());
                $notice_result = false;
            }
        }

        return $notice_result;
    }


    /**
     * 检查用户验证码
     * @param string $account 账号
     * @param string $scene 场景
     * @param string $code 验证码
     * @param bool $is_read 是否已读
     * @param int $uid
     * @return bool
     */
    public static function checkUserCode($account, $scene, $code, $is_read = true, $uid = 0)
    {
        $where = [];
        $where['account'] = $account;
        $where['scene'] = $scene;
        $where['code'] = $code;
        if($uid) $where['uid'] = $uid;

        // 不可多渠道同时发送验证码
        $noticeRow = UserNoticeLog::where($where)->where([
            'status'=>1,
            'is_read'=>0,
            'is_captcha'=>1
        ])->field(['id,scene,code,expire_time'])
            ->findOrEmpty()
            ->toArray();

        if (!$noticeRow) return false;

        $time = time();

        if ($noticeRow['expire_time'] <= $time) return false;

        if ($is_read) {
            UserNoticeLog::update([
                'is_read' => 1,
                'read_time' => $time
            ], ['id' => $noticeRow['id']]);
        }

        return true;
    }
}